package com.course.masterservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.http.BasePageResponse;
import com.course.component.util.CopyUtil;
import com.course.component.util.SnowFlake;
import com.course.dto.reqs.RequestDictionariesInfo;
import com.course.entity.Category;
import com.course.entity.Dictionaries;
import com.course.entity.DictionariesInfo;
import com.course.masterservice.DictionariesInfoMasterService;
import com.course.service.DictionariesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/22 18:09
 * @Description: **********************
 */
@Service
public class DictionariesInfoMasterServiceImpl implements DictionariesInfoMasterService {

    @Autowired
    private DictionariesInfoService dictionariesInfoService;
    @Override
    public BasePageResponse<DictionariesInfo> list(RequestDictionariesInfo requestDictionariesInfo) {
        QueryWrapper<DictionariesInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dic",requestDictionariesInfo.getDic());
        IPage<DictionariesInfo> list = dictionariesInfoService.page(new Page<>(requestDictionariesInfo.getPage().intValue(),requestDictionariesInfo.getPageSize().intValue()),queryWrapper);
        return new BasePageResponse<>(list.getTotal(),list.getRecords());
    }

    @Override
    public Boolean del(RequestDictionariesInfo requestDictionariesInfo) {
        if(requestDictionariesInfo.getId() == null){
            QueryWrapper<DictionariesInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dic",requestDictionariesInfo.getDic());
            return dictionariesInfoService.remove(queryWrapper);
        }else{
            return dictionariesInfoService.removeById(requestDictionariesInfo.getId());
        }
    }

    @Override
    public Boolean update(RequestDictionariesInfo requestDictionariesInfo) {
        DictionariesInfo updateData = CopyUtil.copy(requestDictionariesInfo,DictionariesInfo.class);
        if(ObjectUtils.isEmpty(updateData.getId())){
            updateData.setId(new SnowFlake().nextId());
        }
        return dictionariesInfoService.saveOrUpdate(updateData);
    }
}
